package org.happy.tester.hitest;

import org.happy.tester.common.constant.CommonConstant;
import org.happy.tester.common.http.OkHttpClientUtil;
import org.happy.tester.common.http.OkHttpRequestProvider;
import org.happy.tester.hitest.enums.PointcutTypeEnum;
import org.happy.tester.hitest.support.annotation.Pointcut;
import org.happy.tester.hitest.support.listener.HiTestInvokedMethodListener;
import org.happy.tester.hitest.support.listener.HiTestMethodInterceptor;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname App
 * @Description 0侵入mock例子
 * @Date 2020/6/23 10:29 下午
 * @Created by jc.bai
 */
@Listeners({HiTestMethodInterceptor.class, HiTestInvokedMethodListener.class})
public class App {
    /**
     * collect 模式写法
     *
     * @Pointcut(type = PointcutTypeEnum.COLLECT,
     * className = "org.example.ClassName",
     * method = "exampleMethodName",
     * appIp = "targetAppIp",
     * value = "{\"result\":{\"set\":{\"$.mary\":true}},\"normal\":true,\"excepitonType\":null}",
     * description = "description"
     * )
     */
    @Test
    @Pointcut(type = PointcutTypeEnum.MOCK,
            className = "org.happy.tester.app.service.impl.UserServiceImpl",
            methodName = "getUser",
            appIp = "127.0.0.1",
            value = "{\"result\":{\"set\":{\"$.name\":\"小亮\"}},\"normal\":true,\"excepitonType\":null}",
            description = "mock 模式"
    )
    public void testa() {
        OkHttpClientUtil okHttpClientUtil = new OkHttpClientUtil();
        OkHttpRequestProvider requestProvider = new OkHttpRequestProvider();
        requestProvider.setRootPath("http://localhost:8081");
        requestProvider.setPath("/app/get");
        Map<String, String> headers = new HashMap<>();
        headers.put("user-agent", CommonConstant.DEFAULT_USER_AGENT);
        requestProvider.setHeaders(headers);
        okHttpClientUtil.setRequestProvider(requestProvider);
        okHttpClientUtil.setParamMap(new HashMap<>());
        String result = okHttpClientUtil.doSyncGet();
        System.out.println(result);
    }

}
